package com.lc.ibps.platform.rest.msg;

import org.eclipse.jetty.util.log.Log;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.common.wechat.model.Text;
import com.lc.ibps.common.wechat.model.Wechat;
import com.lc.ibps.common.wechat.model.WechatMsgType;
import com.lc.ibps.common.wechat.utils.WechatUtil;
import com.lc.ibps.web.controller.BaseApiController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 微信服务
 *
 * <pre>
 *  
 * 构建组：ibps-platform-webapi
 * 作者：zhongjh
 * 邮箱：zjh20140614@163.com
 * 日期：2018年7月24日-下午2:11:01
 * 版权：广州流辰信息技术有限公司版权所有
 * </pre>
 */
@RequestMapping(value = "/api/msg/wechatService")
@Api(value = "/wechatService", tags = "系统管理-微信服务")
@RestController
public class WechatController extends BaseApiController{

	public WechatController() {

	}

	@ApiOperation(value = "获取token", notes = "获取token")
	@RequestMapping(value = "/getToken", method = RequestMethod.GET)
	public APIResult<String> getToken(
			@ApiParam(name = "getTokenUrl", value = "请求地址", required = false) @RequestParam(name = "getTokenUrl", required = false) String getTokenUrl,
			@ApiParam(name = "corpid", value = "企业Id", required = false) @RequestParam(name = "corpid", required = false) String corpid,
			@ApiParam(name = "corpsecret", value = "应用secretId", required = false) @RequestParam(name = "corpsecret", required = false) String corpsecret) {

		getTokenUrl = StringUtils.isEmpty(getTokenUrl) ? AppUtil.getProperty("wechat.getToken") : getTokenUrl;
		corpid = StringUtils.isEmpty(corpid) ? AppUtil.getProperty("wechat.corpid") : corpid;
		corpsecret = StringUtils.isEmpty(corpsecret) ? AppUtil.getProperty("wechat.corpsecret") : corpsecret;

		String token = WechatUtil.getToken(getTokenUrl, corpid, corpsecret);
		APIResult<String> result = new APIResult<>();
		result.setData(token);
		result.setMessage("成功获取token！");
		return result;
	}

	@ApiOperation(value = "发送微信消息", notes = "发送微信消息")
	@RequestMapping(value = "/sendMessage", method = RequestMethod.GET)
	public APIResult<String> sendMessage(
			@ApiParam(name = "wcAccount", value = "微信号", required = true) @RequestParam(name = "wcAccount", required = true) String wcAccount,
			@ApiParam(name = "content", value = "内容", required = true) @RequestParam(name = "content", required = true) String content) {
		System.out.println("test微信推送--用户:"+wcAccount);
		APIResult<String> result = new APIResult<>();
		Wechat wechat = new Wechat();
		try {
			wechat.setTouser(wcAccount);
			wechat.setMsgtype(WechatMsgType.checkType(AppUtil.getProperty("wechat.messageType")));
			wechat.setAgentid(AppUtil.getProperty("wechat.agentId"));
			wechat.setText(new Text(content));
			WechatUtil.sendMessage(wechat, "", "");
			result.setMessage("成功消息成功");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "添加用户信息失败！", e);
		}

		return result;
	}

}
